package com.kefan.reptilemanagement.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kefan.reptilemanagement.common.entities.Application;
import com.kefan.reptilemanagement.common.entities.Illegal;
import com.kefan.reptilemanagement.common.entities.SysUser;
import com.kefan.reptilemanagement.common.errorHandler.BizException;
import com.kefan.reptilemanagement.mapper.ApplicationMapper;
import com.kefan.reptilemanagement.service.ApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kefan.reptilemanagement.service.IllegalService;
import com.kefan.reptilemanagement.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author sjj
 * @since 2024-05-03
 */
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {

    @Autowired
    SysUserService sysUserService;
    @Autowired
    IllegalService illegalService;
    @Override
    public Application add(Application application) {
        QueryWrapper<Illegal> wrapper=new QueryWrapper<>();
        wrapper.like("url",application.getUrl());
        Illegal illegal=illegalService.getOne(wrapper);
        if(illegal!=null)
        {
            throw new BizException("爬取网站非法！");
        }
        application.setCreated(LocalDateTime.now());
        application.setUpdated(LocalDateTime.now());
        //1待处理2处理中3已完成4取消
        application.setStatus(1);
        //获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户
        SysUser sysUser = sysUserService.getByUsername(username);
        application.setUserId(sysUser.getId());
        save(application);
        return application;
    }

    @Override
    public Application exchange(Application application) {
        Application old=getById(application.getId());
        if(old==null)
        {
            throw new BizException("申请不存在！");
        }
        application.setUpdated(LocalDateTime.now());
        updateById(application);
        return application;
    }

    @Override
    public Application delete(Long id) {
        Application old=getById(id);
        if(old==null)
        {
            throw new BizException("申请不存在！");
        }
        removeById(id);
        return old;
    }

    @Override
    public List<Application> getApplicationList(Long userId, Integer status,String name) {
        QueryWrapper<Application> wrapper=new QueryWrapper<>();
        if(userId!=null)
        {
            wrapper.eq("user_id",userId);
        }
        if(status!=null)
        {
            wrapper.eq("status",status);
        }
        if(name!=null)
        {
            wrapper.eq("name",name);
        }
        return list(wrapper);
    }

    @Override
    public Application cancelApplication(Long id) {
        Application old=getById(id);
        if(old==null)
        {
            throw new BizException("申请不存在！");
        }
        old.setStatus(4);
        old.setUpdated(LocalDateTime.now());
        updateById(old);
        return old;
    }

    @Override
    public Application acceptApplication(Long id) {
        //获取当前用户名
        String username = (String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        //获取当前用户
        SysUser sysUser = sysUserService.getByUsername(username);
        Application old =getById(id);
        if(old==null)
        {
            throw new BizException("申请不存在！");
        }
        else if(old.getStatus()==2)
        {
            throw new BizException("申请已在处理中！");
        }
        else if(old.getStatus()==3)
        {
            throw new BizException("申请已完成处理！");
        }
        else if(old.getStatus()==4)
        {
            throw new BizException("申请已取消！");
        }
        old.setStatus(2);
        old.setTechId(sysUser.getId());
        old.setUpdated(LocalDateTime.now());
        updateById(old);
        return old;
    }
}
